package com.wwz.leetcode;

import org.junit.Test;
/**
 * @description: x的平方根
 * @author: wwz
 * @date: 2023-10-22 10:20:33
 */
public class LeetCode69 {
    @Test
    public void test() {
        System.out.println(mySqrt(2147395599));
    }

    public int mySqrt(int x) {
        int l = 0, r = x, ans = 0;
        while (l <= r) {
            int mid = (r+l)/ 2;
            if ((long)mid * mid <= x) {
                ans = mid;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return ans;
    }

    public int mySqrt2(int x) {
        int l = 0, r = x, ans = -1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if ((long) mid * mid <= x) {
                ans = mid;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return ans;
    }

}
